<template>
	<div class="sw_system_container">
		<div class="sw_table_container">
			<el-table ref="table" size="medium" :data="data" stripe height="100%" class="index_table_wrapper">
				<el-table-column type="index" label="序号" width="54" align="center"></el-table-column>
				<el-table-column label="编号" prop="filed1" width="106"></el-table-column>
				<el-table-column label="课题名称" prop="name" min-width="240"></el-table-column>
				<el-table-column label="课题类型" prop="type" min-width="100" :formatter="topicTypeFormat"></el-table-column>
				<el-table-column label="研究领域" prop="sector" min-width="150">
					<template slot-scope="{ row }">
						{{ row.sector }}
						<template v-if="!!row.filed4">({{ row.filed4 }})</template>
					</template>
				</el-table-column>
				<el-table-column label="申请人" prop="createName" min-width="100"></el-table-column>
				<el-table-column label="操作" :width="$controlWidth(tableButton) + 35" v-if="$isHasAuth(tableButton)">
					<template slot-scope="{ row }">
						<el-button type="text" v-hasPermi="'53721b967faa49b4810cd21681de29f4'" @click="handleSendMail(row.id)">发送提醒</el-button>
						<el-button type="text" v-hasPermi="'7832d34b768d4cab83e451743fae1194'" @click="handleIgnoreData(row.id)">忽略</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<slot></slot>
	</div>
</template>

<script>
import { mapGetters } from 'vuex';
import { mailSendMarvel } from '@/api/business/tbMailMessage';
import { configMailIgnore } from '@/api/business/tbMailIgnore';
export default {
	props: {
		data: { type: Array, default: () => [] }
	},
	computed: {
		...mapGetters(['dictEnums']),
		tableButton() {
			return ['7832d34b768d4cab83e451743fae1194', '53721b967faa49b4810cd21681de29f4'];
		}
	},
	methods: {
		//格式化
		topicTypeFormat(data) {
			let topics = this.dictEnums['PROJECT_TYPE'];
			let info = topics.find((n) => n.value === data.type);
			return !info ? '--' : info.msg;
		},

		//管理员 - 课题详情
		handleLookData(id) {
			let query = { id };
			this.$router.push({ path: '/manager/project/detail', query });
		},

		//管理员 - 发送邮件
		handleSendMail(id) {
			let that = this;
			that.$confirm('此操作将发送成果提醒邮件, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.sendMailRequest(id);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//发送成果提醒请求
		async sendMailRequest(id) {
			let result = await mailSendMarvel(id).then((res) => res.data);
			if (result.code === '0') {
				this.$emit('refresh');
				this.$showCustomMessage('success', `发送成果提醒成功！`);
			}
		},

		//管理员 - 忽略成果提醒
		async handleIgnoreData(id) {
			let that = this;
			that.$confirm('此操作将忽略成果提醒, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.ignoreDataRequest(id);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//忽略成果请求
		async ignoreDataRequest(id) {
			let params = { cpId: id, type: '2' };
			let result = await configMailIgnore(params).then((res) => res.data);
			if (result.code === '0') {
				this.$emit('refresh');
				this.$showCustomMessage('success', `设置成果提醒忽略成功！`);
			}
		}
	}
};
</script>

<style scoped></style>
